package com.itboye.hutouben.activity.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.presenter.SystemMessgeAPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.OnPasswordInputFinish;
import com.itboye.hutouben.util.PasswordView;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 设置支付密码（完成设置）
 *
 */
public class ZhiFuPassSheZhiTwoActivity extends BaseActivity implements Observer{
    TextView add_shap_title_tv,add_lift_tv;
    ImageView close_icon;
    PasswordView pwdView;// 支付密码
    String NewsPwd;
    Intent intent;
    String pass;
    SystemMessgeAPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhi_fu_pass_she_zhi_two);
        add_shap_title_tv.setVisibility(View.GONE);
        add_lift_tv.setText(R.string.shezhi_zhifu_pass);
        presenter=new SystemMessgeAPresenter(this);

        intent = this.getIntent();
        if (intent != null) {
            pass = intent.getStringExtra("pass");
        }

        final PasswordView pwdView = (PasswordView) findViewById(R.id.pwd_view);

        // 添加密码输入完成的响应
        pwdView.setOnFinishInput(new OnPasswordInputFinish() {
            @Override
            public void inputFinish() {
                // 输入完成后跳转
                // 也就是说——>实现你的交易逻辑什么的在这里写
                NewsPwd=pwdView.getStrPassword();
                if(NewsPwd.equals(pass)){
                    presenter.onWallet(IsUtilUid.isUid(),NewsPwd);

                }else{
                    ByAlert.alert("两次密码不一致");
                }


            }
        });
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.Wallet_success) {
                SPUtils.put(ZhiFuPassSheZhiTwoActivity.this, null,Const.PaySecret, pass);
                Intent intent=new Intent(MyApplcation.ctx,ZhiFuMimaDuanXinaYanZhengActivity.class);
                intent.putExtra("NewsPwd",NewsPwd);
                startActivity(intent);
                finish();
            }
        }
        if (entity != null) {
            if (entity.getEventType() == presenter.Wallet_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
